﻿using System;
using System.Collections.Generic;
using System.Linq;
using QuickGraph;
using Xray.Common;

namespace Xray.Viewer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : IModelUpdate
    {
        private readonly HostWrapper<IContainerXrayService> _containerService;
        private readonly BidirectionalGraph<object, IEdge<object>> _graphToVisualize;
        private const string WaitingForUpdate = "No Components";

        public IBidirectionalGraph<object, IEdge<object>> GraphToVisualize
        {
            get { return _graphToVisualize; }
        }

        public MainWindow()
        {
            _containerService = new HostWrapper<IContainerXrayService>(
                new ContainerXrayService(this),
                new Uri("http://localhost:8999/xray"));

            _graphToVisualize = new BidirectionalGraph<object, IEdge<object>>();
            _graphToVisualize.AddVertex(WaitingForUpdate);

            InitializeComponent();

            cboLayout.SelectedIndex = 0;
            graphLayout.LayoutAlgorithmType = ((System.Windows.Controls.ComboBoxItem)cboLayout.SelectedValue).Content.ToString();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            _containerService.Dispose();
            base.OnClosing(e);

        }

        public void UpdateModel(ModelDeltas modelDelta)
        {
            lock (_graphToVisualize)
            {
                foreach (Component newComponent in modelDelta.NewComponents)
                {
                    _graphToVisualize.AddVertex(newComponent);
                }

                foreach (Pair newDependent in modelDelta.NewDependents)
                {
                    _graphToVisualize.AddEdge(new Edge<object>(newDependent.Component, newDependent.Dependent));
                }

                if (_graphToVisualize.ContainsVertex(WaitingForUpdate) && _graphToVisualize.VertexCount > 1)
                {
                    _graphToVisualize.RemoveVertex(WaitingForUpdate);
                }
            }

            //graphLayout.Relayout();
            //graphLayout.UpdateLayout();
            graphLayout.LayoutAlgorithmType = ((System.Windows.Controls.ComboBoxItem)cboLayout.SelectedValue).Content.ToString();
        }

        public void Clear()
        {
            IList<IEdge<object>> edgesToRemove = _graphToVisualize.Edges.ToList();

            foreach (IEdge<object> edge in edgesToRemove)
            {
                _graphToVisualize.RemoveEdge(edge);    
            }

            List<object> verticesToRemove = _graphToVisualize.Vertices.ToList();
            foreach (object vertex in verticesToRemove)
            {
                _graphToVisualize.RemoveVertex(vertex);
            }

            _graphToVisualize.AddVertex(WaitingForUpdate);
        }

        private void CboLayoutSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            graphLayout.LayoutAlgorithmType = ((System.Windows.Controls.ComboBoxItem)cboLayout.SelectedValue).Content.ToString();

            
        }

        private void BtnClearClick(object sender, System.Windows.RoutedEventArgs e)
        {
            _containerService.HostInstance().ClearModel();
        }
    }
}
